package org.ichow.question
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.filters.ColorMatrixFilter;
	import flash.utils.getDefinitionByName;
	import org.ichow.debug.Debug;
	import org.ichow.question.plugins.*;
	
	/**
	 * ...
	 * @author M.ichow
	 */
	public class Question extends Sprite
	{
		public static const grayFilter : ColorMatrixFilter = new ColorMatrixFilter([ .33 , .33 , .33 , 0 , 0 , .33 , .33 , .33 , 0 , 0 , .33 , .33 , .33 , 0 , 0 , 0 , 0 , 0 , 1 , 0 ] );
		public static var QUESTION_PATH : String = "";
		
		public static var activatedPlugins : Object = {};
		
		private var data : Object;
		private var container : DisplayObjectContainer;
		private var skin : Sprite;
		private var plugins : Array;
		
		private var questionName : String = "q_$_mc";
		private var __answerMc : MovieClip;
		private var _isCheck : Boolean = false;
		
		public function Question()
		{
		}
		
		/**
		 * initialize
		 */
		protected function init() : void
		{
			//组件数组
			plugins = [];
			//题目初始坐标
			skin.x = skin.y = 2;
			//题目组件数据
			var _ary : Array = data[ "data" ];
			//循环根据组件创建代码
			for ( var i : int = 0 , len : int = _ary.length ; i < len ; i++ )
			{
				var d : Object = _ary[ i ];
				
				if ( !( d.type in activatedPlugins ) )
				{
					Debug.log( "No Activate Plugins: " + d.type , this , Debug.LEVEL_FATAL );
					//throw( new Error( "No Activate Plugins: " + d.type ) );
					return;
				}
				
				var plugin : QuestionPlugin = new ( activatedPlugins[ d.type ] as Class )();
				
				var _skin : DisplayObjectContainer = null;
				if ( d.localName == "self" )
				{
					_skin = skin;
				}
				else if ( skin.getChildByName( d.localName ) != null )
				{
					_skin = skin.getChildByName( d.localName ) as DisplayObjectContainer;
				}
				else if ( skin.getChildByName( questionName.replace( "$" , d.localName ) ) != null )
				{
					_skin = skin.getChildByName( questionName.replace( "$" , d.localName ) ) as DisplayObjectContainer;
				}
				else
				{
					_skin = skin;
				}
				plugin.bind( _skin , d );
				plugins[ i ] = plugin;
			}
			
			__answerMc = skin.getChildByName( "answer_mc" ) as MovieClip;
		}
		
		/**
		 * 绑定
		 * @param	skin			:	题目
		 * @param	container		:	容器
		 * @param	data			:	数据
		 */
		public function bind( skin : Sprite , container : DisplayObjectContainer , data : Object , isInit : Boolean = true ) : void
		{
			this.skin = skin;
			this.container = container;
			this.data = data;
			if ( isInit )
				init();
		}
		
		/**
		 * 启用
		 * 添加题目到容器并启用题目的组件
		 */
		public function active() : void
		{
			if ( skin is DisplayObject )
				container.addChild( skin );
			if ( __answerMc && __answerMc.currentFrame == 2 )
				return;
			for each ( var i : QuestionPlugin in plugins )
			{
				i.active();
			}
		}
		
		/**
		 * 取消
		 * 移除题目并取消组件
		 */
		public function cancel() : void
		{
			if ( container.contains( skin ) )
				container.removeChild( skin );
			for each ( var i : QuestionPlugin in plugins )
			{
				i.cancel();
			}
		}
		
		/**
		 * 屏蔽
		 * 取消组件显示答案
		 */
		public function screen() : Boolean
		{
			if ( __answerMc && __answerMc.currentFrame == 1 )
			{
				for each ( var i : QuestionPlugin in plugins )
				{
					i.cancel();
				}
				__answerMc.gotoAndStop( 2 );
				_isCheck = true;
				return false;
			}
			return true;
		}
		
		/**
		 * reset the question
		 */
		public function reset() : void
		{
			for each ( var i : QuestionPlugin in plugins )
			{
				i.reset();
			}
			_isCheck = false;
			if ( __answerMc )
				__answerMc.gotoAndStop( 1 );
		}
		
		/**
		 * 检查题目
		 * @return
		 */
		public function check() : Boolean
		{
			for each ( var i : QuestionPlugin in plugins )
			{
				if ( !i.check() )
					return false;
			}
			return true;
		}
		
		/**
		 *
		 * @return
		 */
		public function get score() : Number
		{
			var _socre : Number = 0;
			for each ( var i : QuestionPlugin in plugins )
			{
				_socre += i.score;
			}
			return _socre;
		}
		
		public function get isCheck() : Boolean
		{
			return _isCheck;
		}
	
	}

}